
/**   
*    
* 项目名称：a_yongan   
* 类名称：OutsourcingInController   
* 类描述：   
* 创建人：liubo   
* 创建时间：2018年6月19日 下午3:29:51    
* 修改时间：2018年6月19日 下午3:29:51   
* 修改备注：   
* @version    
*    
*/
package com.yongan.sales.web;

import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yongan.sales.entity.SalesOrder;
import com.yongan.sales.entity.SalesOut;
import com.yongan.sales.service.SalesOrderService;
import com.yongan.sales.service.SalesOutService;
import com.yongan.user.dto.UserResult;
import com.yongan.util.Execution;
import com.yongan.util.PageBean;
import com.yongan.util.SerialNumber;

/**
 * @author liubo
 *
 */
@Controller
@RequestMapping("/salesout")
public class SalesOutController {

	@Autowired
	private SalesOutService salesOutService;
	@Autowired
	private SalesOrderService salesOrderService;
	
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String salesoutadd(Model model,HttpServletRequest request) {
		String number = "CK"+SerialNumber.Getnum();
		model.addAttribute("number", number);
		
		List<SalesOrder> salesOrderlist = null;
		String page = request.getParameter("currentPage");
		
		String dialog = request.getParameter("dialog");//为选择订货单单独增加
		System.out.println(dialog+"=============");
		int currentPage;
		int totalRecords;
		if (page != null) {
			currentPage = Integer.parseInt(page);
		} else {
			currentPage = 1;
		}
		PageBean<SalesOrder> pb = new PageBean<SalesOrder>();
		
		totalRecords = salesOrderService.getCount();
		pb.setCurrentPage(currentPage);
		pb.setTotalRecords(totalRecords);
		pb.setTotalPages(pb.getTotalRecords() % pb.getPagesize() == 0 ? pb
				.getTotalRecords() / pb.getPagesize() : pb.getTotalRecords()
				/ pb.getPagesize() + 1);
		if (totalRecords > 0) {
			int start = (pb.getCurrentPage() - 1) * pb.getPagesize();
			int end = pb.getCurrentPage() * pb.getPagesize();
			if (end > totalRecords - 1) {
				end = totalRecords;
			}
			salesOrderlist = salesOrderService.getSalesOrderList(start, end);
			//model.addAttribute("pb", pb);
		}
		model.addAttribute("pb", pb);
		model.addAttribute("salesOrderlist", salesOrderlist);
		
		return "salesoutadd";
	}
	@RequestMapping(value = "/adddo", method = RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public UserResult<Execution> salesoutadddo(SalesOut salesOut,Model model) {
		System.out.println(salesOut.toString());
		salesOut.setSalesOutId(UUID.randomUUID().toString());
		Execution rs = salesOutService.executeSalesOut(salesOut, "", "1233213");
		int rsn = rs.getState();
		if (rsn == 1) {
			return new UserResult<Execution>(true, rs);
		}
		return new UserResult<Execution>(false, "");
	}
	
	@RequestMapping(value = "/salesoutlist", method = RequestMethod.GET)
	public String salesoutcontent(Model model,HttpServletRequest request) {
		List<SalesOut> salesOutlist = null;
		String page = request.getParameter("currentPage");
		int currentPage;
		int totalRecords;
		if (page != null) {
			currentPage = Integer.parseInt(page);
		} else {
			currentPage = 1;
		}
		PageBean<SalesOut> pb = new PageBean<SalesOut>();
		
		totalRecords = salesOutService.getCount();
		pb.setCurrentPage(currentPage);
		pb.setTotalRecords(totalRecords);
		pb.setTotalPages(pb.getTotalRecords() % pb.getPagesize() == 0 ? pb
				.getTotalRecords() / pb.getPagesize() : pb.getTotalRecords()
				/ pb.getPagesize() + 1);
		if (totalRecords > 0) {
			int start = (pb.getCurrentPage() - 1) * pb.getPagesize();
			int end = pb.getCurrentPage() * pb.getPagesize();
			if (end > totalRecords - 1) {
				end = totalRecords;
			}
			salesOutlist = salesOutService.getSalesOutList(start, end);
			//model.addAttribute("pb", pb);
		}
		model.addAttribute("pb", pb);
		model.addAttribute("salesOutlist", salesOutlist);
		return "salesoutlist";
	}
	
	@RequestMapping(value = "/{salesOutId}/detail", method = RequestMethod.GET)
	public String salesoutdetail(@PathVariable("salesOutId") String salesOutId, Model model) {
		if (!(salesOutId.equals("")|salesOutId==null)) {
			SalesOut salesOut = salesOutService.getSalesOutById(salesOutId);
			model.addAttribute("salesOut", salesOut);
		}
		return "salesoutdetail";
	}
	
	@RequestMapping(value = "/update", method = RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public UserResult<Execution> salesoutupdate(SalesOut salesOut, Model model) {
		Execution rs = salesOutService.executeSalesOut(salesOut, salesOut.getSalesOutId(), "123321231");
		int rsn = rs.getState();
		if (rsn == 1) {
			return new UserResult<Execution>(true, rs);
		}
		return new UserResult<Execution>(false, "");
	}
	
	@RequestMapping(value = "/{salesOutId}/delete", method = RequestMethod.GET, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public UserResult<Execution> salesoutDelete(
			@PathVariable("salesOutId") String salesOutId,Model model) {
		if (!(salesOutId.equals("")|salesOutId==null)) {
			Execution rsdelete = salesOutService.executeSalesOut(null, salesOutId, "1212331232");
			int rsn = rsdelete.getState();
			if (rsn == 1) {
				return new UserResult<Execution>(true, rsdelete);
			}
		}
		return new UserResult<Execution>(false, "");
	}
	
	@RequestMapping(value = "/{salesOrderId}/value", method = RequestMethod.GET, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public UserResult<SalesOrder> salesoutvalue(
			@PathVariable("salesOrderId") String salesOrderId, Model model) {
		if (!(salesOrderId.equals("")|salesOrderId==null)) {
			SalesOrder salesOrder = salesOrderService.getSalesOrderById(salesOrderId);
			if (salesOrder != null) {
				return new UserResult<SalesOrder>(true, salesOrder);
			}
		}
		return new UserResult<SalesOrder>(false, "");
	}
}
